<?php

/**
 * Created by PhpStorm.
 * User: wukent
 * Date: 2017/9/26
 * Time: 下午3:56
 */
class PayNotifyAction extends Action
{


    protected $mainMod;
    protected $usersMod;
    protected function _initialize() {
        $this->mainMod = D("OfflineOrder");
        $this->usersMod = D("Users");
    }

    /*
     * 支付宝异步验签
     * */
    public function notifycheck(){

        vendor('alipaysdk.aop.AopClient');
        $aop = new AopClient;
        //公钥
        $aop->alipayrsaPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApV/jU0SgPXt8uryI2TxpAwBP+gbf6IZRrpjWoa4LYoVQxjXqXumm17d0vSHrYblQAlehQiQnm6lW2PXeIa7+pAtghUn9qdAg6eM/46r8nQ5iOVnQUtcHwxUChdm5C6OjzeHpFN0qxpoYZKpUs2COOwE5IiQkaTiJkP0I6TC1D5qoap2StCpcKJFPnXvjybLQjesbznTx5KPrAh+G4cUTCHDPiHZSd+iV7QPRnkN5gtQjpO187+hD80VzhwHVKNHpWALzOQ728W8wJcFvaIGyy1Oku0WPDNsHPEcCtp4D+L2wN/Zy2BjwW0JjdXYOi8QpXMW818JDKHBsFsZKMNlj0QIDAQAB';
        $flag = $aop->rsaCheckV1($_POST, NULL, "RSA");

        $output='<?php
              $info='.var_export($_POST, true).';
              ?>';

        file_put_contents('./log.php', $output);

        if($this->mainMod->upstatus($this->_post('out_trade_no'),1)){
            $res = 1;
        }else {
            $res = 0;
        }

        $orderinfo = $this->mainMod->getInfo($this->_post('out_trade_no'));
        $userinfo = $this->usersMod->getInfo($orderinfo['user_id']);

        $points = $userinfo['rank_points']-$orderinfo['actual_points'];

        if ($this->usersMod->uppoints($orderinfo['user_id'],$points)){
            $res = 1;
        }else{
            $res = 0;
        }




        if ($res == 1){
            echo 'success';
        }


    }

    // public function WxNotify(){
    //     vendor('Weixin.lib.WxPay.Notify');


    //     $orderinfo = $this->mainMod->getInfo($this->_post('out_trade_no'));
    //     $userinfo = $this->usersMod->getInfo($orderinfo['user_id']);

    //     $points = $userinfo['rank_points']-$orderinfo['actual_points'];

    //     if ($this->usersMod->uppoints($orderinfo['user_id'],$points)){
    //         $res = 1;
    //     }else{
    //         $res = 0;
    //     }

    //     if ($res == 1){
    //         echo 'success';
    //     }

    // }

     public function WxNotify()
    {
        vendor('Weixin.lib.WxPay.Notify');
        $xml = file_get_contents('php://input');
        $arr = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        //用户http_build_query()将数据转成URL键值对形式
        $sign = http_build_query($arr);
        //md5处理
        $sign = md5($sign);
        //转大写
        $sign = strtoupper($sign);
        //验签名。默认支持MD5
        if ( $sign === $arr['sign']) {
//            校验返回的订单金额是否与商户侧的订单金额一致。修改订单表中的支付状态。
            $orderinfo = $this->mainMod->getInfo($arr['out_trade_no']);
            $userinfo = $this->usersMod->getInfo($orderinfo['user_id']);

            $points = $userinfo['rank_points']-$orderinfo['actual_points'];

            if ($this->usersMod->uppoints($orderinfo['user_id'],$points)){
                $res = 1;
            }else{
                $res = 0;
            }
        }
        if ($res == 1){
            $return = ['return_code'=>'SUCCESS','return_msg'=>'OK'];
            $xml = '<xml>';
            foreach($return as $k=>$v){
                $xml.='<'.$k.'><![CDATA['.$v.']]></'.$k.'>';
            }
            $xml.='</xml>';
            echo $xml; 
        }
    }
}